#include "database.h"
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>
#include <QCryptographicHash> //md5加密
#include <QMessageBox>


Database::Database(QObject *parent) : QObject(parent)
{
    createDBCon();
}

void Database::createDBCon()
{
    db = QSqlDatabase::addDatabase(QString("QMYSQL"));
    db.setHostName("127.0.0.1");
    db.setPort(3306);
    db.setUserName("root");
    db.setPassword("123456");
    db.setDatabaseName("io");

    if (!db.open())
    {
        qDebug() << "连接失败！";
        qDebug() << db.lastError().text();
        return ;
    }
    qDebug() << "Available database drivers:" << QSqlDatabase::drivers();
    qDebug() << "连接成功";
    this->query = QSqlQuery(db);

}

bool Database::isAccExist(QString name)
{

    QSqlDatabase db = QSqlDatabase::database();
    QSqlQuery query(db);
    query.prepare("select * from user_info where username = ?");
    query.addBindValue(name);

    if (query.exec()) {
        qDebug()<<"查询成功";
        if (query.next()) {
            // 有匹配的记录
            return true;
        } else {
            // 没有匹配的记录
            return false;
        }

    } else {
        // 查询执行失败
        qDebug() << "查询失败:" << query.lastError().text();
        return false;
    }
}

bool Database::isPwdExist(QString name,QString pwd)
{
    //1、对密码进行md5加密处理
    QString pwdMD5;
    QByteArray str;
    str = QCryptographicHash::hash(pwd.toLatin1(), QCryptographicHash::Md5); // 进行MD5加密
    pwdMD5.append(str.toHex()); // 将加密结果转换为十六进制字符串
    qDebug()<<pwdMD5;
   //2、获取数据库中密码
    QSqlQuery query;
    query.prepare("select * from user_info where username = ?");
    query.addBindValue(name);
    if (!query.exec())
    {
        qDebug() << "密码查询失败！" << query.lastError().text();
        return false;
    }

    QString password;
    while (query.next()) {
         password = query.value("password").toString();
    }
    if(pwdMD5 != password)
    {
        return false;
    }

    return true;
}

int Database::addUser(UserInfo *user)
{
    qDebug() << user->getUserName();
    //2、判断账号是否存在
    if(isAccExist(user->getUserName()))
    {
        //用户已存在
        return 101;
    }
    QSqlDatabase db = QSqlDatabase::database();
    QSqlQuery query(db);
    query.prepare("insert into user_info (username,password) "
                  "values(:username,:password)");

    query.bindValue(":username",user->getUserName());
    query.bindValue(":password",user->getPassword());
    if (query.exec())
    {
        //用户注册成功
        qDebug()<<"注册成功";
        return 200;
    }
    else
    {
        // 查询执行失败
        qDebug() << "查询失败:" << query.lastError().text();
        return 404 ;
    }
}


//QVector<PortInfo>  Database::getPortInfo()

 QVector<ConfigInfo*> Database::getConfigInfoInfo()
{
    this->query.prepare("select * from configinfo");
    this->query.exec();
    QVector<ConfigInfo*> v;
    while (this->query.next())
    {
        ConfigInfo* configinfo =new ConfigInfo();
        configinfo->setPortNo(this->query.value("port_no").toInt());
        configinfo->setCrExplained(this->query.value("cr_explained").toString());
        configinfo->setIfExplained(this->query.value("if_explained").toString());
        configinfo->setLinksNum(this->query.value("links_num").toInt());
        configinfo->setProtocol(this->query.value("protocol").toInt());
        configinfo->setPeerHost(this->query.value("peerhost").toString());
        configinfo->setPeerPort(this->query.value("peerport").toInt());
        configinfo->setConnect(this->query.value("connect").toString());
        configinfo->setDisconnet(this->query.value("disconnet").toString());
        configinfo->setFreetime(this->query.value("freetime").toInt());
        v.push_back(configinfo);
    }
    return  v;
}

PortInfo* Database::getPortInfoByPortNo(int portNo)
{
    this->query.prepare("select * from portinfo where port_no = ?");
    this->query.addBindValue(portNo);

    if(!this->query.exec())
    {
        qDebug()<<"获取配置信息失败";
        return nullptr;
    }
    PortInfo* portinfo;
    while (this->query.next())
    {
        int portNo = query.value("port_no").toInt();
        int comNo = query.value("com_no").toInt();
        QString comNm = query.value("com_nm").toString();
        int baudrate = query.value("baudrate").toInt();
        int databit = query.value("databit").toInt();
        int stopbit = query.value("stopbit").toInt();
        int checkbit = query.value("checkbit").toInt();
        QString flowcontrol = query.value("flowcontrol").toString();
        int rxtime = query.value("rx_time").toInt();
        int txtime = query.value("tx_time").toInt();
        portinfo = new PortInfo(portNo,comNo,comNm,baudrate,databit,
                                stopbit,checkbit,flowcontrol,rxtime,txtime);
    }
    return portinfo;
}

void Database::setLoginName(QString str)
{
    this->login_name = str;
}

QString Database::getLoginName()
{
    return  this->login_name;
}
